package com.len.controller;

import com.len.base.BaseController;
import com.len.core.annotation.Log;
import com.len.entity.Behavior;
import com.len.entity.BehaviorField;
import com.len.exception.MyException;
import com.len.service.BehaviorFieldService;
import com.len.service.BehaviorService;
import com.len.util.BeanUtil;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author songxiangyang
 * @date 2019/02/19.
 * 行为业务
 */
@Controller
@RequestMapping(value = "/behavior")
public class BehaviorController extends BaseController {

    @Autowired
    private BehaviorService behaviorService;
    @Autowired
    private BehaviorFieldService behaviorFieldService;

    @GetMapping(value = "showBehavior")
    @RequiresPermissions(value = "behavior:show")
    public String showBehavior(Model model) {
        return "behavior/behaviorList";
    }

    @ApiOperation(value = "/showBehaviorList", httpMethod = "GET", notes = "展示行为")
    @GetMapping(value = "showBehaviorList")
    @ResponseBody
    @RequiresPermissions("behavior:show")
    public ReType showBehaviorList(Behavior behavior, Model model, String page, String limit) {
        return behaviorService.show(behavior, Integer.valueOf(page), Integer.valueOf(limit));
    }

    @ApiOperation(value = "/selectBehaviorList", httpMethod = "GET", notes = "展示行为")
    @GetMapping(value = "selectBehaviorList")
    @ResponseBody
    @RequiresPermissions("behavior:show")
    public ReType selectBehaviorList(Behavior behavior) {
        return behaviorService.selectBehaviorList(behavior);
    }

    @GetMapping(value = "showAddBehavior")
    public String goAddBehavior(Model model) {
        return "behavior/add-behavior";
    }

    @ApiOperation(value = "/addBehavior", httpMethod = "POST", notes = "添加行为")
    @Log(desc = "添加行为")
    @PostMapping(value = "addBehavior")
    @ResponseBody
    public JsonUtil addBehavior(Behavior behavior) {
        if (StringUtils.isEmpty(behavior.getTbName())) {
            JsonUtil.error("行为名称不能为空");
        }
        JsonUtil j = new JsonUtil();
        try {
            behavior.setCreateDate(new Date());
            behavior.setUpdateDate(new Date());
            behaviorService.insertSelective(behavior);
            j.setMsg("保存成功");
        } catch (MyException e) {
            j.setMsg("保存失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }
//
    @GetMapping(value = "updateBehavior")
    public String updateBehavior(String id, Model model, boolean detail) {
        if (StringUtils.isNotEmpty(id)) {
            Behavior behavior = behaviorService.selectByPrimaryKey(id);
            model.addAttribute("behavior", behavior);
        }
        model.addAttribute("detail", detail);
        return "behavior/update-behavior";
    }

    @ApiOperation(value = "/updateBehavior", httpMethod = "POST", notes = "更新行为")
    @Log(desc = "更新行为")
    @PostMapping(value = "updateBehavior")
    @ResponseBody
    public JsonUtil updateBehavior(Behavior behavior) {
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setFlag(false);
        if (behavior == null) {
            jsonUtil.setMsg("获取数据失败");
            return jsonUtil;
        }
        try {
            Behavior oldBehavior = behaviorService.selectByPrimaryKey(behavior.getId());
            BeanUtil.copyNotNullBean(behavior, oldBehavior);
            oldBehavior.setUpdateDate(new Date());
            behaviorService.updateByPrimaryKeySelective(oldBehavior);

            jsonUtil.setFlag(true);
            jsonUtil.setMsg("修改成功");
        } catch (MyException e) {
            jsonUtil.setMsg("修改失败");
            e.printStackTrace();
        }
        return jsonUtil;
    }

    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除行为")
    @Log(desc = "删除行为", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "del")
    @ResponseBody
    @RequiresPermissions("behavior:del")
    public JsonUtil del(String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonUtil.error("获取数据失败");
        }
        BehaviorField behaviorField = new BehaviorField();
        behaviorField.setBehaviorId(id);
        JsonUtil j = new JsonUtil();
        try {
            int count = behaviorFieldService.selectCountByCondition(behaviorField);
            if (count > 0) {
                return JsonUtil.error("已分配给行为属性，删除失败");
            }
            behaviorService.deleteByPrimaryKey(id);
            j.setMsg("删除成功");
        } catch (MyException e) {
            j.setMsg("删除失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

}
